package dp.gr03.payroll.application;

import java.util.GregorianCalendar;

import dp.gr03.payroll.exception.PayRollDateFormatExcpeption;

public class DateParser
{
    public static GregorianCalendar parseDate(String date) throws PayRollDateFormatExcpeption
    {
        String[] d = date.split("-");
        if (d.length == 3)
        {
            try
            {
                int year = Integer.parseInt(d[0]);
                int month = Integer.parseInt(d[1]) - 1;
                int day = Integer.parseInt(d[2]);
                return new GregorianCalendar(year, month, day);
            } catch (NumberFormatException nfe)
            {
                throw new PayRollDateFormatExcpeption("Please enter valid numbers");
            }
        } else
        {
            throw new PayRollDateFormatExcpeption("The date format (" + date + ") is invalid: valid format is yyyy-mm-dd");
        }
    }
}
